<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none" placeholder="请输入搜索内容"></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResult.length !==0">
      <view class="sugg-item" v-for="(item,i) in searchResult" :key="i" @click="goToDetail(item)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史记录 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clean"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for=" (item,i) in histories" :key="i" @click="goToGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: '',
        keyWord: '',
        searchResult: [], //搜索的结果列表
        historyList: [],
        
      };
    },
    methods: {
      input(e) {
        //输入事件的处理函数
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.keyWord = e
          this.getSearchList()
        }, 500)
      },
      async getSearchList() {
        if (this.keyWord == '') {
          this.searchResult = []
          return
        }
        const {
          data: res
        } = await uni.$http.get('https://api-ugo-web.itheima.net/api/public/v1/goods/qsearch', {
          query: this.keyWord
        })
        if (res.meta.status != 200) return uni.$showMsg()
        this.searchResult = res.message
        this.saveHistorySearch()
      },
      goToDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      //保存搜索记录
      saveHistorySearch() {
        this.historyList.push(this.keyWord)
        //进行持久化的存储在本地
        uni.setStorageSync('keyWord' , JSON.stringify(this.histories))
      },
      //清除历史记录
      clean() {
        this.historyList = []
        uni.setStorageSync('keyWord','')
      },
      goToGoodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + item
        })
      }
    },
    computed:{
      histories() {
        return [...new Set([...this.historyList].reverse())]
      }
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('keyWord')) || '[]'
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      display: flex;
      justify-items: center;
      justify-content: space-between;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;

      .goods-name {
        white-space: nowrap; //不允许换行
        overflow: hidden; //溢出部分隐藏
        text-overflow: ellipsis; //超出内容用三个小圆点表示
        font-size: 14px;
      }
    }
  }

  .sugg-item:active {
    color: red;
  }
  .history-box {
    padding: 0 5px;
    .history-title {
      display: flex;
      justify-content: space-between;
      height: 40px;
      align-items: center;
      font-size: 14px;
      border-bottom: 1px solid #efefef;
    }
    .history-list {
      display: flex;
      flex-wrap: wrap;
      .uni-tag {
        marign-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
